/**
 * 微信官方文档
 * https://developers.weixin.qq.com/miniprogram/dev/reference/api/Page.html
 */

// 保存原本的 Page 对象
let orgainPage = Page
/**
 * Page 对象基础类
 * @param {Object} app (当前 Page 对象)
 * @returns
 */
Page = function (_obj) {
    let app = getApp()
    // 加载配置文件
    let conf = require('../../../app.config')

    // 拷贝一个新的页面对象
    let orgainData = Object.assign({}, _obj);

    /** [扩展] Data 对象 */
    let _dataList = {
        pageReady: false,
        cdn: conf.app.cdnPath,
        PageAni: 'opacity:0;',
    };
    _obj.data = Object.assign(_obj.data, _dataList);

    _obj.app = getApp()
    _obj.api = require('../../../common/js/api')
    _obj.math = require('../../../common/js/plugin/math')
    _obj.com = require('../../../common/js/plugin/com')
    _obj.rds = require('../../../common/js/plugin/wxredis')
    _obj.tongji = _obj.app.tongji
    _obj.system = wx.getSystemInfoSync()
    _obj.config = conf

     ////////////////////////////////////////////////////////////////////////////////
     //虎年提示弹窗 pophuniantip Alert
    _obj.showPophunianTip = function(obj){
        this.selectComponent('#pophuniantip').showTipAlert(obj)
    }
    //虎年授权弹窗 pophunianauth Alert
    _obj.showPophunianAuth = function(obj){
        this.selectComponent('#pophunianauth').showAuthAlert(obj)
    }
    

    _obj.setTitle = function (_val) {
        wx.setNavigationBarTitle({
            title: _val
        })
    }
    _obj.goto = function (_url, _reLaunch) {
        if (_reLaunch)
            wx.reLaunch({
                url: _url
            })
        else
            wx.redirectTo({
                url: _url
            })
    }
    _obj.navto = function (_url) {
        wx.navigateTo({
            url: _url
        })
    }
    _obj.navback = function (_idx) {
        wx.navigateBack({
            delta: _idx
        })
    }
    // 提示框
    _obj.alert = function (txt, showcancel, callback) {
        txt = txt + ''
        let opts = {
            content: txt,
            success: callback || function () {},
            showCancel: showcancel || false
        };
        wx.showModal(opts);
    }
    //Loading 封装
    _obj.loading = {
        show: function (txt, needMask) {
            wx.showLoading({
                title: txt,
                mask: needMask
            });
        },
        hide: function () {
            wx.hideLoading();
        }
    }
    // 判断是否是X XR等手机
    _obj.iPX = function () {
        let isX = false
        if (
            (_obj.system.screenHeight == 812 && _obj.system.screenWidth == 375) ||
            (_obj.system.screenHeight == 896 && _obj.system.screenWidth == 414)
        ) {
            isX = true
        }
        return isX
    }

    _obj.onLoad = function (options) {
        // 
        if (!conf.app.canShare) wx.hideShareMenu();

        this.setData({
            options: options
        })

        if (conf.monitor.pitaya.appKey == '')
            console.error('================ 请设置PITAYA当前项目专用KAY ================')

        // 公共逻辑写在此处
        if (orgainData.onLoad && typeof orgainData.onLoad === 'function') {
            orgainData.onLoad.call(this, options);
        }
    }

    _obj.onShow = function (options) {
        if (typeof orgainData.onShow === 'function') {
            orgainData.onShow.call(this, options);
        }
        // setTimeout(() => {
        //     this.setData({
        //         isReady: true
        //     })
        // }, 300);
    }

    _obj.onReady = function (options) {
        // 公共逻辑写在此处
        this.setData({
            pageReady: true
        })
        if (typeof orgainData.onReady === 'function') {
            orgainData.onReady.call(this, options);
        }
    }
    // 活力值/勋章
    _obj.check_xunzhang = function (text, point, isSuccess, cb) {
        if (point != 0 || isSuccess != 0) {
        let text_ = ''
        if (point != 0 && isSuccess != 0) {
            // text_ = `${text}成功,活力值+${point}\r\n${xunzhang==0?'':'并获得'+xunzhang+'枚'+text+'勋章！'}`;
            text_ = `${text}成功,活力值+${point}\r\n活力值可以兑换产品和礼品哦！`;
        } else {
            if (point != 0) {
            text_ = `${text}成功,活力值+${point}\r\n活力值可以兑换产品和礼品哦！`;
            }
            if (isSuccess != 0) {
            text_ = `${text}成功`;
            }
        }

        // if(text=='发帖'){
        //     this.setEvent("wenzanglinqu",'getarticlething');
        // }
        // if(text=='点赞'){
        //     this.setEvent("dianzanlingqu",'likeget');
        // }
        // if(text=='评论'){
        //     this.setEvent("pinglunlingqu",'commentget');
        // }

        // if(text=='分享'){
        //     this.setEvent("zhuanfalingqu",'shareget');
        // }

        wx.showModal({
            content: text_,
            confirmText: "立即领取",
            showCancel: false,
            success: function () {
                console.log(isSuccess,'isSuccess -- - -- -- -- >')
            if (isSuccess != 0) {
                wx.navigateTo({
                url: '/pages/details/course/course',
                })
            }
            cb && cb(isSuccess)
            }
        });
        }
    }
    _obj.onHide = function (options) {
        // 公共逻辑写在此处
        if (typeof orgainData.onHide === 'function') {
            orgainData.onHide.call(this, options);
        }
    }

    _obj.onUnload = function () {
        // 公共逻辑写在此处

        if (orgainData.onUnload !== undefined && typeof orgainData.onUnload === 'function') {
            orgainData.onUnload.call(this);
        }
    }

    _obj.onPullDownRefresh = function () {
        // 公共逻辑写在此处

        if (orgainData.onPullDownRefresh !== undefined && typeof orgainData.onPullDownRefresh === 'function') {
            orgainData.onPullDownRefresh.call(this);
        }
    }

    _obj.onReachBottom = function () {
        // 公共逻辑写在此处

        if (orgainData.onReachBottom !== undefined && typeof orgainData.onReachBottom === 'function') {
            orgainData.onReachBottom.call(this);
        }
    }

    _obj.onShareAppMessage = function (options) {
        // 公共逻辑写在此处

        let _share = conf.app.shareSettings;

        if (orgainData.onShareAppMessage !== undefined && typeof orgainData.onShareAppMessage === 'function') {
            _share = orgainData.onShareAppMessage.call(this, options)
            if (conf.monitor.baidu.enable) {
                if (!_share.title || !_share.imageUrl) _share = conf.app.shareSettings;
            }
        }

        return _share;
    }

    _obj.onResize = function () {
        // 公共逻辑写在此处
        if (orgainData.onResize !== undefined && typeof orgainData.onResize === 'function') {
            orgainData.onResize.call(this);
        }
    }

    return orgainPage(_obj);
}